﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using AEM.Data;

namespace AEM.Controls
{
    /// <summary>
    /// Line.xaml 的交互逻辑
    /// </summary>
    public partial class LiftLine : UserControl
    {
        public Action<string, Data.Enum.LiftStatus> StatusChanged;
        public LiftLine()
        {
            InitializeComponent();
            StatusChanged = doStatusChanged;
        }

        private void doStatusChanged(string liftnum, Data.Enum.LiftStatus ls)
        {
            Dispatcher.Invoke(new Action(() =>
            {
                InLine il = (InLine)lift_inline.FindName("l" + liftnum);
                if (null == il) return;
                il.LiftStatus = ls;
            }));
        }

        private static void OnLineNameChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            if (args.NewValue != args.OldValue)
            {
                if (string.IsNullOrEmpty(args.NewValue as string)) return;
                if (null == Config.LiftInftList) return;
                IEnumerable<Data.Model.LiftInfo> result = from Data.Model.LiftInfo row in Config.LiftInftList
                                                          where row.LiftLine == Convert.ToInt32(args.NewValue)
                                                          orderby row.LiftLineSerial
                                                          select row;
                if (null == result || result.Count() < 1) { (sender as LiftLine).lift_line.Text = (sender as LiftLine).TryFindResource("NoID") as string; return; }
                (sender as LiftLine).lift_line.Text = ((sender as LiftLine).TryFindResource("Line") as string) + " " + (string)args.NewValue;
                InitModelInfo(sender as LiftLine, result);
            }
        }

        private static void InitModelInfo(LiftLine m, IEnumerable<Data.Model.LiftInfo> rs)
        {
            m.lift_inline.Children.Clear();
            foreach (Data.Model.LiftInfo r in rs)
            {
                InLine il = new InLine();
                m.RegisterName("l" + r.LiftNum, il);
                il.Name = "l" + r.LiftNum;
                il.LiftName = r.ShortName;
                il.LiftType = (Data.Enum.LiftType)r.LiftType;
                il.LiftStatus = Data.Enum.LiftStatus.NC;
                m.lift_inline.Children.Add(il);
            }
        }

        /// <summary>
        /// 数据库内电梯线路
        /// </summary>
        public static readonly DependencyProperty LineNameProperty =
            DependencyProperty.Register("LineName", typeof(string), typeof(LiftLine),
            new FrameworkPropertyMetadata("", new PropertyChangedCallback(OnLineNameChanged)));

        /// <summary>
        /// 数据库内电梯线路
        /// </summary>
        public string LineName
        {
            get { return (string)GetValue(LineNameProperty); }
            set { SetValue(LineNameProperty, value); }
        }


    }
}
